# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 11/5/2021 13:14
desc: 结构化相关功能
'''

import requests
import base.globalvars as glo

def __init():
    from base.get_config import get_header_file
    from base.getdata import get_header
    global url1,url2,header1,header2
    url1 = glo.get_value('url1')
    url2 = glo.get_value('url2')
    header1 = get_header(get_header_file(), "header1")
    header2 = get_header(get_header_file(), "header2")

def structurize(file_id, model_id, element_ids) :
    """

    :param data_source: 数据
    :param element_ids: 元素id列表
    :param file_id:
    :param model_id:

    :return:
    """
    __init()

    url = url1 + "/api/v1/Output/struct2"

    payload = {
        "FileId":file_id,
        "ModelId":model_id,
        "ElementIds":",".join([str(id) for id in element_ids])
    }

    res = requests.post(url, headers = header2, json=payload)

    assert res.status_code==200,"结构化失败！ %s"%payload


def get_struct_data(file_id, model_id, element_id):
    """获取结构化数据

    :param file_id: 文件id
    :param model_id: 模型id
    :param element_id: 元素id

    :return: 结构化数据的json对象
    """
    __init()
    url = url2 + "/api/v1/ex/OutputEx/filter/join2?ModelId=%s&ElementId=%s&FileId=%s"%(model_id, element_id, file_id)

    res = requests.get(url, headers = header1)

    assert res.status_code == 200, "获取结构化数据失败：file id %s, model id %s, element_id %s"%(file_id, model_id, element_id)

    return res.json()
